import Toast from '@vant/weapp/toast/toast'
import { saveProfile, getProfile, getScore, getScoreList } from '../../apis/user'
import { assetsUrl } from '../../config/env'
import { getGoodsDetail, getGoodsList } from '../../apis/goods'

const defaultAvatarUrl =
    'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
    data: {
        assetsUrl,
        userInfo: {
            avatarUrl: defaultAvatarUrl,
            nickName: '',
            openId: ''
        },
        userScore: 0,
        scoreIndex: 1,
        compList: []
    },
    gotoEdit() {
        wx.navigateTo({
            url: '../userinfo/index'
        })
    },
    gotoManager() {
        wx.navigateTo({
            url: '../manager/index'
        })
    },
    gotoOrder() {
        wx.navigateTo({
            url: '../order-list/index'
        })
    },
    gotoAddress() {
        wx.navigateTo({
            url: '../address-list/index'
        })
    },
    gotoScore() {
        wx.navigateTo({
            url: '../score/index'
        })
    },
    async getScores() {
        const res = await getScoreList({
            pageNum: 1,
            pageSize: 1000
        })
        const obj = res.list?.find(item => item.openId === this.data.userInfo.openId)
        this.setData({
            userScore: obj?.score,
            scoreIndex: obj?.rank
        })
    },
    async getCompList() {
        const res = await getGoodsList({
            pageNum: 1,
            pageSize: 4
        })
        this.setData({
            compList: res.list
        })
    },
    gotoDetail(e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/detail/index?id=' + id
        })
    },
    async getInfo() {
        const res = await getProfile()
        console.log(res)
        this.setData({
            'userInfo.avatarUrl': res.headUrl,
            'userInfo.nickName': res.name,
            'userInfo.openId': res.openId
        })
    },
    async getMyScore() {
        const res = await getScore()
        console.log(res)
        console.log(res)
    },
    onLoad() {
        this.getInfo()
        this.getMyScore()
        this.getCompList()
        this.getScores()
    }
})
